<template>
  <div>
    <TableHeader
      :buttons="['refresh', 'comSearch', 'columnDisplay']"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
    <Table ref="tableRef" @action="baTable.onTableAction" />
  </div>
</template>
<script lang="ts" setup>
import { ref, onUnmounted, onMounted, provide, toRaw } from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi } from "@/utils/common"; // 导入表格api方法生成器
import { EquipMCarbonType } from "@/api/backend/index";
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import type { TableColumn, TableRow } from "@/utils/baTable"; //表格列行类型
import useCurrentInstance from "@/utils/useCurrentInstance";
import { useCarbon } from "@/store/backend/EquipM/activeCarbon";
import { useNavTabs } from "@/store/navTabs";
import { useRoute } from "vue-router";

const tableRef = ref();
const placeholder = ref<string>("模糊搜索");
const carbon = useCarbon();
const navTabs = useNavTabs();
const route = useRoute();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + EquipMCarbonType)
      ? (navTabs as any).pageSizeMap.get(route.path + EquipMCarbonType)
      : 10;
  }
  return 10;
};

//eid对标设备id id查询日志详情
const baTable = new baTableClass(new baTableApi(EquipMCarbonType), {
  pk: "id",
  column: [
    { type: "selection", align: "center", operator: false },
    {
      show: filedShowOff("id"),
      label: "id",
      prop: "id",
      align: "center",
      operator: false,
      operatorPlaceholder: "模糊搜索",
      width: 70,
    },
    {
      show: filedShowOff("eid"),
      label: "设备id",
      prop: "eid",
      align: "center",
      operator: false,
      operatorPlaceholder: "模糊搜索",
      width: 70,
    },
    {
      show: filedShowOff("ecode"),
      label: "设备编号",
      prop: "ecode",
      align: "center",
      operator: false,
      operatorPlaceholder: "模糊搜索",
    },
    {
      show: filedShowOff("type"),
      label: "操作事件",
      prop: "type",
      align: "center",
      render: "tag",
      custom: { 0: "success", 1: "info", 2: "warning", 3: "danger" },
      replaceValue: {
        0: "开机",
        1: "关机",
        2: "更换滤芯",
        3: "告警",
      },
    },
    {
      show: filedShowOff("eventValue"),
      label: "告警原因",
      prop: "eventValue",
      align: "center",
      operator: false,
      width: 160,
    },
    {
      show: filedShowOff("eventTime"),
      label: "更换滤芯时间",
      prop: "eventTime",
      align: "center",
      operator: false,
      operatorPlaceholder: "模糊搜索",
    },
  ],
  filter: {
    eid: carbon.item.eid,
    type: "2",
    pageSize: pageSizeKeep(),
  },
});

onMounted(() => {
  const { proxy } = useCurrentInstance();
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(EquipMCarbonType);
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
</script>
<style lang="scss" scoped></style>
